package com.partner.frame.support.util;

import java.io.UnsupportedEncodingException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;

public class RsaUtil {
	private static KeyFactory keyFactory;
	private static Cipher cipher;
	private static final String pubkey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJTNP5p23AQZ3J+tuZrEGfDqWRk4y33Z52k5tOB2C0qsg/LCm2wUEGQk2/EQcGa6vx8FHt9ErTfB2Zz6GC1rdW0CAwEAAQ==";
	private static final String prikey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAlM0/mnbcBBncn625msQZ8OpZGTjLfdnnaTm04HYLSqyD8sKbbBQQZCTb8RBwZrq/HwUe30StN8HZnPoYLWt1bQIDAQABAkAIEAL1PKF5O2l/4WrgHuYE7r27Jx+nhwoJ4o2RZ/P+rLsZJTAghU8Hx38G2r0ebWZkSMEk1gH4KZzSBTMJBqPBAiEA0Z9KgOqFNAfoqR3Qc8sTmSdlkP8Zy6WaokPYg3+ZffECIQC1uSu884EyEl/l8w9BFXOdhOnsM7YteM6LN9K1LSijPQIgCKbre1CyuMgOyVQqs2zRXUPsBEUTOn2WWVY1jI/qtPECIQCt3AxBicMIkdXYHbRxc0Ga6egXyN26IYOSZIUeO6dOjQIgOVsLr1Co18b1q+3/a+tWTIpC+Jzgw4q02LLXFmeuv0o=";

	// 明文117位，密文172位
/**
 * 私钥加密
 * @param src
 * @return
 */
	public static String enCodeWithPri(String src) {
		// 私钥加密、公钥解密 ---- 加密
		try {
			PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
					Base64.decodeBase64(prikey));
			keyFactory = KeyFactory.getInstance("RSA");
			PrivateKey privateKey = keyFactory
					.generatePrivate(pkcs8EncodedKeySpec);
			cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(Cipher.ENCRYPT_MODE, privateKey);
			byte[] result = cipher.doFinal(src.getBytes("utf-8"));
			return Base64.encodeBase64String(result);
			// System.out.println("私钥加密、公钥解密 ---- 加密:" +
			// Base64.encodeBase64String(result));
		} catch (Exception e) {
			// TODO 异常处理
			e.printStackTrace();
			return null;
		}
	}
/**
 * 公钥解密
 * @param enCodesrc
 * @return
 */
	public static String deCodeWithPub(String enCodesrc) {
		// 私钥加密、公钥解密 ---- 加密
		try {
			// 3.私钥加密、公钥解密 ---- 解密
			X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
					Base64.decodeBase64(pubkey));
			keyFactory = KeyFactory.getInstance("RSA");
			PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
			cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(Cipher.DECRYPT_MODE, publicKey);
			byte[] result = cipher.doFinal(Base64.decodeBase64(enCodesrc));
			return new String(result);
			// System.out.println("私钥加密、公钥解密 ---- 解密:" + new String(result));
		} catch (Exception e) {
			// TODO 异常处理
			e.printStackTrace();
			return null;
		}
	}
/**
 * 私钥解密	
 * @param src
 * @return
 */
	public static String deCodeWithPri(String src) {
		// 私钥加密、公钥解密 ---- 加密
		try {
			PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(prikey));
			keyFactory = KeyFactory.getInstance("RSA");
			PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
			cipher = Cipher.getInstance("RSA");
			cipher.init(Cipher.DECRYPT_MODE, privateKey);
			byte[] result = cipher.doFinal(Base64.decodeBase64(src));
			System.out.println("公钥加密、私钥解密——解密：" + new String(result));
			return new String(result) ;
			
		} catch (Exception e) {
			// TODO 异常处理
			e.printStackTrace();
			return null;
		}
	}	
	/**
	 * 公钥加密
	 * @param enCodesrc
	 * @return
	 */
	public static String enCodeWithPub(String src) {
		
		try {
			// 公钥加密、私钥解密 ---- 加密
			
			X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(pubkey));
			keyFactory = KeyFactory.getInstance("RSA");
			PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
			cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(Cipher.ENCRYPT_MODE, publicKey);
			byte[] result = cipher.doFinal(src.getBytes("utf-8"));
			System.out.println("公钥加密、私钥解密——加密 : " + Base64.encodeBase64String(result));
			return Base64.encodeBase64String(result);
			// System.out.println("私钥加密、公钥解密 ---- 解密:" + new String(result));
		} catch (Exception e) {
			// TODO 异常处理
			e.printStackTrace();
			return null;
		}
	}	
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
	//1.初始化密钥
	/*
	KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
	keyPairGenerator.initialize(512);
	KeyPair keyPair = keyPairGenerator.generateKeyPair();
	RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
	RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
	System.out.println("Public Key : " + Base64.encodeBase64String(rsaPublicKey.getEncoded())); 
	System.out.println("Private Key : " + Base64.encodeBase64String(rsaPrivateKey.getEncoded()));
	*/
	
	//test
	String src = "fzlSB";
	//System.out.println(enCodeWithPub(src));
	deCodeWithPri(enCodeWithPub(src));
	System.out.println(deCodeWithPri("I6qjc0T0pNvXmeJ5g3mRSC9tWXWV7TF0omFqaKRKdsN8bdUvesUaYHoBTocg9u3CoDDNB9cHJtMheEPw88c1YQ=="));
	//System.out.println(Base64.encodeBase64String(src.getBytes("utf-8")));
	//System.out.println(enCodeWithPri(src));
	//System.out.println(enCodeWithPri(src).length());
	//System.out.println(deCodeWithPub(enCodeWithPri(src)));
}
}
